package com.zlg.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zlg.entity.Users;
import com.zlg.service.UsersService;


@Controller
@RequestMapping("/users")
public class UserController {
	@Autowired
	public UsersService userService;
	
	/*@RequestMapping("/show")
	public String showUser(Model model){
		List<Users> list = new ArrayList<>();
		list.add(new Users(1,"张三",20));
		list.add(new Users(2,"李四",22));
		list.add(new Users(3,"王五",23));
		model.addAttribute("list", list);
		return "userList";
	}*/
	/**
	 * 页面跳转
	 * @param users
	 * @return
	 */
	@RequestMapping("/{page}")
	public String showPage(@PathVariable String page){
		return page;
	}
	
	/**
	 * 新增用户
	 * @param users
	 * @return
	 */
	@RequestMapping("/addUser")
	public String addUser(Users users){
		userService.addUser(users);
		return "ok";
	}
	
	/**
	 * 查询全部用户
	 */
	@RequestMapping("/selectAll")
	public String findUsersAll(Model model){
		List<Users> list = userService.findUsersAll();
		model.addAttribute("list", list);
		return "showUsers";
	}
	
	@RequestMapping("/findUserById")
	public String findUserById(Integer id,Model model){
		Users user = userService.findUserById(id);
		model.addAttribute("user", user);
		return "updateUser";
	}
	
	@RequestMapping("/editUser")
	public String updateUsers(Users users){
		userService.updateUser(users);
		return "ok";
	}
	
	@RequestMapping("/delUser")
	public String delUser(Integer id){
		userService.deleteUserById(id);
		return "redirect:/users/selectAll";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
